//
//  TuijianController.m
//  网易新闻
//
//  Created by dllo on 15/10/5.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "TuijianController.h"
#import "MJRefresh.h"
#import "TuijianCell_a.h"
#import "TuijianCell_b.h"
#import "TuijianCell_c.h"
#import "TuijianCell_d.h"
#import "AFTool.h"
#import "TuijianModel.h"
#import "DetailsController.h"
#import "Settings.h"
@interface TuijianController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *modArr;
@property(nonatomic, assign)NSInteger cellheight;
@property(nonatomic, retain)DetailsController *result;
@end

@implementation TuijianController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.cellheight = 44;
        [self createTableView];
        
    }
    return self;
}

- (void)viewWillLayoutSubviews{
    [self.tableView.header beginRefreshing];
    self.tableView.contentOffset = CGPointMake(0, 0);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 565) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    [self.tableView registerClass:[TuijianCell_a class] forCellReuseIdentifier:@"TuijianCell_a"];
    [self.tableView registerClass:[TuijianCell_b class] forCellReuseIdentifier:@"TuijianCell_b"];
    [self.tableView registerClass:[TuijianCell_c class] forCellReuseIdentifier:@"TuijianCell_c"];
    [self.tableView registerClass:[TuijianCell_d class] forCellReuseIdentifier:@"TuijianCell_d"];
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handelDataWithURL:@"http://c.3g.163.com/recommend/getSubDocNews?passport=&devId=862095021675571&size=20&version=5.3.3&from=yuedu&net=wifi" WithType:0];
        [self.tableView.header endRefreshing];
    }];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self handelDataWithURL:@"http://c.3g.163.com/recommend/getSubDocNews?passport=&devId=862095021675571&size=20&version=5.3.3&from=yuedu&net=wifi" WithType:1];
        [self.tableView.footer endRefreshing];
    }];
    
    [self notificationHandle];
}

- (void)handelDataWithURL:(NSString *)url WithType:(NSInteger)type{
    [AFTool GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        if (type == 0) {
            self.modArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:@"推荐"]];
            [[Settings shareSettings]writeToDBWithDic:tempDic WithName:url];
        }else if (type == 1){
            [self.modArr addObjectsFromArray:[tempDic objectForKey:@"推荐"]];
        }
        [tempDic release];
        [self.tableView reloadData];
    } fail:^(NSError *error) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:[[Settings shareSettings]dicSelectFromDB:url]];
        if (![tempDic isEqual:nil]) {
            self.modArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:@"推荐"]];
            [tempDic release];
            [self.tableView reloadData];
        }
        NSLog(@"404(TuijianController)");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TuijianModel *mod = [TuijianModel modWithDic:self.modArr[indexPath.row]];
    if ([mod.template_a isEqualToString:@"pic1"]) {
        TuijianCell_a *cell = [[TuijianCell_a alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TuijianCell_a"];
        cell.mod = mod;
        self.cellheight = cell.height;
        return cell;
    }else if([mod.template_a isEqualToString:@"normal"]){
        TuijianCell_b *cell = [[TuijianCell_b alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TuijianCell_b"];
        cell.mod = mod;
        self.cellheight = cell.height;
        return cell;
    }else if([mod.template_a isEqualToString:@"pic31"]) {
        TuijianCell_c *cell = [[TuijianCell_c alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TuijianCell_c"];
        cell.mod = mod;
        self.cellheight = cell.height;
        return cell;
    }else{
        TuijianCell_d *cell = [[TuijianCell_d alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TuijianCell_d"];
        cell.mod = mod;
        self.cellheight = cell.height;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.result = [[DetailsController alloc]init];
    self.result.docid = [self.modArr[indexPath.row]objectForKey:@"docid"];;
    [self.navigationController pushViewController:self.result animated:YES];
    [self.result release];
}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.tableView.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    }];
}

- (void)dealloc{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    [_modArr release];
    [_tableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
